package org.scut.smartgeometry.view.identify_system;

import java.util.List;

/**
 * Created by L on 13-7-2.
 */
public class Line_Unit {
    private double JUDGE_LINE_VALUE = 0.9;

    private SGPath path;
    private List<SGPoint> points;

    public void setPoints(List<SGPoint> points) {
        this.points = points;
    }

    public void setPath(SGPath path) {
        this.path = path;
    }

    public Line_Unit(){

    }

    public Boolean smartG(){
        int length = points.size();
        float xsum = 0;
        float ysum = 0;
        float xx = 0;
        float yy = 0;
        float xy = 0;
        float x_av;
        float y_av;

        for(int i = 0; i< length; i++){
            SGPoint p = points.get(length-1);
            float x = p.getX();
            float y = p.getY();
            xsum += x;
            ysum += y;
        }

        x_av = xsum/length;
        y_av = ysum/length;

        for(int i = 0 ; i < length ; i++){
            SGPoint p = points.get(length-1);
            float x = p.getX();
            float y = p.getY();
            xx += (x-x_av)*(x-x_av);
            yy += (y-y_av)*(y-y_av);
            xy += (x-x_av)*(y-y_av);
        }

        double m = xy /(StrictMath.sqrt(xx * yy));

        if (m > JUDGE_LINE_VALUE || m < -JUDGE_LINE_VALUE){
            return true;
        }
        else return false;
    }
    public void Linepaint(){
        int length = points.size();
        path.reset();
        path.moveTo(points.get(0).getX(), points.get(0).getY());
        path.lineTo(points.get(length-1).getX(), points.get(length-1).getY());
    }
}


